<?php

/**
 * This is the model class for table "{{comment}}".
 *
 * The followings are the available columns in table '{{comment}}':
 * @property integer $id
 * @property integer $resource_id
 * @property string $resource_type
 * @property integer $user_id
 * @property integer $rating
 * @property string $comment
 * @property integer $status
 */
class BSMComment extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{comment}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('resource_id, resource_type, user_id', 'required'),
			array('resource_id, user_id, rating, status', 'numerical', 'integerOnly'=>true),
			array('resource_type', 'length', 'max'=>128),
			array('comment', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, resource_id, resource_type, user_id, rating, comment, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'user'=>array(self::BELONGS_TO, 'BMUser', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'resource_id' => 'Resource',
			'resource_type' => 'Resource Type',
			'user_id' => 'User',
			'rating' => 'Rating',
			'comment' => 'Comment',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('resource_id',$this->resource_id);
		$criteria->compare('resource_type',$this->resource_type,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('rating',$this->rating);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @return CDbConnection the database connection used for this class
	 */
	public function getDbConnection()
	{
		return Yii::app()->bookingdb;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return BSMComment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function getCount($resourceId, $resourceType)
    {
		$criteria = new CDbCriteria;

		$criteria->compare('resource_id', $resourceId);
		$criteria->compare('resource_type', $resourceType);
        
        return self::model()->count($criteria);
    }
    
    public function getList($resourceId, $resourceType)
    {
		$criteria = new CDbCriteria;

		$criteria->compare('resource_id', $resourceId);
		$criteria->compare('resource_type', $resourceType);

        $criteria->with = array(
            'user',
        );

        return self::model()->findAll($criteria);
    }
    
    public static function calcAvgRating($resourceId, $resourceType)
    {
		$criteria = new CDbCriteria;

		$criteria->compare('resource_id', $resourceId);
		$criteria->compare('resource_type', $resourceType);
        
        $criteria->select = new CDbExpression('AVG(rating) AS rating');

        $item = self::model()->find($criteria);
        return $item['rating'];
    }
}
